package com.juzi.wopu.web;




import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;






import com.juzi.wopu.BaseDB;
import com.juzi.wopu.LoggerUtil;
import com.juzi.wopu.pb;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysUser;
import com.juzi.wopu.service.ISysCodeService;
import com.opensymphony.xwork2.ActionContext;



public class CountAction extends CRUDAaction<SysUser> {

	private static final long serialVersionUID = 1L;
	private ISysCodeService sysCodeService;
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}

	@Override
	public void list() {
        
	}

	@Override
	public SysUser getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prepareSave() throws Exception {
		// TODO Auto-generated method stub
		
	}

	
public void Index() throws JSONException, IOException{
	
    HttpServletResponse response=ServletActionContext.getResponse();  
        response.setContentType("text/html");  
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;  
        out = response.getWriter();  
         BaseDB db=new BaseDB();
         Connection con=db.getConnection();
		 String sSql="";
		 JSONObject json= new JSONObject();
	 		try {
	         sSql="select distinct iCompany=(case iCompany when 19 then '中国联合网络通信有限公司四川省分公司' when 1901 then '四川通发电信股份有限公司'when 1902 then '中国联合网络通信有限公司新疆维吾尔自治区分公司' when 1903 then '中国联合网络通信有限公司四川天府信息中心' end),count(iCompany)as num from tSysUser where  dtCode>'2016-09-01'GROUP BY iCompany"; 
	         LoggerUtil.Log(sSql);
	         ResultSet re=BaseDB.OpenSql(con, sSql);
 			JSONArray jsonArray = new JSONArray();
			while(re.next()){
				String Company=re.getString("iCompany");
				String num=re.getString("num");
				 JSONObject js=new JSONObject();
		          js.put("Company", Company);
		          js.put("Num", num);
		          jsonArray.put(js);
			}

			 json.put("data", jsonArray);  
		} catch (Exception e) {
		           e.printStackTrace();
		}
 		    out.write(json.toString());
 	        out.flush();  
 	        out.close();
		}	



public void Seach() throws IOException{
	ActionContext context=ActionContext.getContext();  
	HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
    String Type=request.getParameter("Type");
    String Company=request.getParameter("Company");
    String BeginTime=request.getParameter("BeginTime");
    String EndTime=request.getParameter("EndTime");
    HttpServletResponse response=ServletActionContext.getResponse();  
        response.setContentType("text/html");  
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out;  
        out = response.getWriter();  
        Map<String, SysCode> nameCodeMap=new HashMap<>();
        Map<Long, SysCode> map=new HashMap<>();
        sysCodeService.getSysCodeMap(map, nameCodeMap);//得到sName对应SysCode的map

         BaseDB db=new BaseDB();
         Connection con=db.getConnection();
		 String sSql="";
		 JSONObject json= new JSONObject();
		    if(Type.equals("1")){
		    	//查看新用户	
	    		String Time=pb.getMonday();
		    	if(Company.equals("全部")){
		        	 sSql="select *  from tSysUser where  dtInsert >='"+Time+"' order by dtInsert desc ";
		        }else{
		        	Long iCompany= nameCodeMap.get(Company).getiId();
		        	sSql="select *  from tSysUser where iCompany='"+iCompany+"' and  dtInsert >='"+Time+"' order by dtInsert desc ";
		        }
		    }else if(Type.equals("0")){
		    	//按时间查找
		        if(Company.equals("全部")){
		        	 sSql="select *  from tSysUser where  dtInsert between '"+BeginTime+"' and '"+EndTime+"' order by dtInsert desc ";
		        }else{
		        	Long iCompany= nameCodeMap.get(Company).getiId();
		        	sSql="select *  from tSysUser where iCompany='"+iCompany+"' and dtInsert between '"+BeginTime+"' and '"+EndTime+"' order by dtInsert desc ";
		        }
		    }

        LoggerUtil.Log(sSql);
 		try {
 		         JSONArray jsonArray = new JSONArray();
		         ResultSet rs=BaseDB.OpenSql(con, sSql);
		         int num=0;
            while(rs.next()){
    			 String Name=rs.getString("sUserNo");
    			 String Mobile=rs.getString("sMobile");
    			        Company=rs.getString("iCompany");
           	     String Time=pb.deletehm(rs.getString("dtInsert"));
    			 String IOS=rs.getString("sPhoneType");
    			 if(IOS.equals("AND")||IOS.equals("")||IOS==null){
    				 IOS="安卓系统";
    			 }else{
    				 IOS="苹果系统";
    			 }
    			 if(Company==null||Company.equals("")){
    				 Company="1901";
    			 }
    			 String Sex=rs.getString("sSex");
    	 		 JSONObject js=new JSONObject();
    			          js.put("Name", Name);
    			          js.put("Mobile", Mobile);
    			          js.put("Company", Company);
    			          js.put("Time", Time);
    			          js.put("IOS", IOS);
    			          js.put("Sex", Sex);
    			          jsonArray.put(js);
    			          num+=1;
            }
                json.put("data", jsonArray);          
                json.put("num", num);          
		} catch (SQLException e) {
			    e.printStackTrace();
		}
 		 catch (JSONException ee) {
			    ee.printStackTrace();
		}
 		    out.write(json.toString());
 	        out.flush();  
 	        out.close();
		}	
}
